package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.SalaryDAO;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.SalaryProfile;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.dao.model.UserAccountsDetail;
import com.pentofab.payrollmanagement.dao.model.ViewSalaryProfileDetail;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;

public class ViewSalaryProfileController extends AbstractUIController implements
		FormValidator {
	
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> basic; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> bonus; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> conveyance; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> desc; 
	@FXML
    private TableView<ViewSalaryProfileDetail> displayTable; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> esi; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> hra; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> maxPF; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> medicalAllo; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> pfPer; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> profileName;
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> others;
	@FXML
    private GridPane root; 
	@FXML
    private TableColumn<ViewSalaryProfileDetail, String> specialAllo; 
	UserAccounts logedInUser = null;
	Company company = null;
	
	SalaryDAO salaryDAO = PayrollApplicationFactory.getSalaryDAO();
	
	public void add() {
	
		PayrollApplicationFactory.getMainController().showScreen(MainController.SalaryProfile, true, company,logedInUser);
    
	}
	public void delete() {
		if(displayTable.getSelectionModel().getSelectedIndex() < 0){
			showError("Please select a salary profile to delete");
			return;
		}
		
		ViewSalaryProfileDetail detail =  displayTable.getSelectionModel().getSelectedItem();
		if(!showConfirmDialog("Are sure you want to delete seected profile "+detail.getProfileName())){
			return;
		}
		
		try{
			
			salaryDAO.deleteSalaryProfile(detail.getProfileName());
			
		}catch(Exception e){
			showError(e.getMessage());
			return;
		}
		showInfo("Salary profile ["+detail.getProfileName()+"] deleted successfully.");
		refreshUI();
    }
	public void modify() {
		
		if(displayTable.getSelectionModel().getSelectedIndex() < 0){
			showError("Please select a salary profile to modify");
			return;
		}
		
		ViewSalaryProfileDetail detail =  displayTable.getSelectionModel().getSelectedItem();
		
		if(!showConfirmDialog("Are sure you want to modify selected profile ["+detail.getProfileName()+"].\n All employee salary stracture which uses this profiles will be modified")){
			return;
		}
		
		PayrollApplicationFactory.getMainController().showScreen(MainController.SalaryProfile, true, company,logedInUser,detail);
	}
	
	public void goBack() {

		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}

	}

    public void reset() {
    	refreshUI();
    }


	
	@Override
	public boolean isValid() {
		return false;
	}

	@Override
	public String getValidationMessage() {

		if(profileName.getText().trim().equals("")){
			return "Salary profile name can not be empty.";
		}

		if(desc.getText().trim().equals("")){
			return "Salary profile descrition can not be empty.";
		}
		return "";
	
	}

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return "Salary Profile Viwer";
	}

	@Override
	public void refreshUI(Object... args) {
		
		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = company;
			
			if(args.length > 1 && args[1] instanceof UserAccounts){
				logedInUser = (UserAccounts) args[1];
			}
		}
		
		
		populateSalaryProfiletable();
	}
	private void populateSalaryProfiletable() {
		
		displayTable.getItems().clear();
		ObservableList<ViewSalaryProfileDetail> data = FXCollections.observableArrayList();
		
		PayRollUtility.debug("===================== populateSalaryProfiletablet =====================");

		data.clear();
		try {
			ArrayList<SalaryProfile> salaryProfileList = salaryDAO.getSalaryProfiles();
			System.out.println("accList : " + salaryProfileList);
			if(salaryProfileList == null){
				return;
			}
			data.addAll(ViewSalaryProfileDetail.getViewSalaryProfileDetailList(salaryProfileList));
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		PayRollUtility.debug("data  : " + data);
		displayTable.setItems(data);

		PayRollUtility.debug("===================== populateSalaryProfiletable =====================");

	}
	@Override
	public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
		
		iniTableProps();
	}
	private void iniTableProps() {
		
		displayTable.setEditable(false);
	     bonus.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("bonus"));
	     conveyance.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("conveyance"));
	     desc.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("desc"));
	     esi.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("esi"));
	     maxPF.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("maxPF"));
	     medicalAllo.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("medicalAllo"));
	     pfPer.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("pfPer"));
	     profileName.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("profileName"));
		basic.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("basic"));
		hra.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("hra"));
		specialAllo.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("specialAllo"));
		others.setCellValueFactory(new PropertyValueFactory<ViewSalaryProfileDetail, String>("others"));
	}
	
}
